package com.zlt.filter;

import com.alibaba.fastjson.JSON;
import com.zlt.service.UserService;
import com.zlt.service.impl.UserServiceImpl;
import com.zlt.util.ActiveUserUtil;
import com.zlt.util.StringUtil;
import com.zlt.vo.Result;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author wbh
 * @version 1.0
 * @description: TODO
 * @date 2022/8/11 15:52
 */
@WebFilter(filterName = "LoginFilter", value = "/*",
        initParams = @WebInitParam(name = "urls",
                value = "/user/info,/user/infoUser,/company/createCompany,/company/all,/company/allHr,/company/reviewedHr,/vip/pay,/vip/vipState,/vip/chargeRecord," +
                        "/seeker/resume/createOnlineResume,/seeker/resume/updateAdvantage,/seeker/resume/updateExpectedPosition,/seeker/resume/updateEducation,/seeker/resume/updateInternship," +
                        "/seeker/resume/updateSalary,/seeker/resume/queryOnlineResume,/seeker/resume/uploadResume,/seeker/resume/queryAllResume," +
                        "/seeker/resume/removeResume,/seeker/resume/previewResume,/seeker/resume/setupDefaultResume,/seeker/resume/sendResume," +
                        "/recruiter/delivery/showJobResume,/recruiter/delivery/batchPass,/recruiter/delivery/batchReject,/recruiter/delivery/passJobResume,/recruiter/delivery/rejectJobResume,/recruiter/delivery/onlineResume,/recruiter/delivery/jobType" +
                        "/recruiter/menu/menu,/company/createCompany,/company/all,/recruiter/job/publishJob,/recruiter/job/all," +
                        "/recruiter/offer/addOffer,/recruiter/offer/sendOffer,/recruiter/offer/allOffer," +
                        "/recruiter/offer/deleteOffer,/recruiter/offer/updateOffer,/recruiter/offer/sendOfflineOffer," +
                        "/recruiter/interview/addInterview,/recruiter/interview/selectAllInterview,/recruiter/interview/updateInterview," +
                        "/recruiter/delivery/showJobResume,/recruiter/delivery/updateResumeState,/recruiter/delivery/onlineViewResume,/recruiter/delivery/batchPass," +
                        "/recruiter/delivery/batchReject,/recruiter/delivery/passJobResume,/recruiter/delivery/rejectJobResume,/recruiter/delivery/jobType," +
                        "/recruiter/delivery/onlineResume,/seeker/job/collect,/seeker/job/checkCollect," +
                        "/question/submit," +"/question/advise" ))
public class LoginFilter implements Filter {
    
    
    private UserService userService = new UserServiceImpl();
    
    private List<String> urls;
    
    public void destroy() {
        System.out.println("LoginFilter is destroy");
    }
    
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("LoginFilter is doFilter");
        
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String requestURI = request.getRequestURI();
        
        requestURI = requestURI.substring(request.getContextPath().length());
        
        if (urls.contains(requestURI)) {// 这个路径需要登录才可以访问
            String token = request.getHeader("token");
            if (StringUtil.isNotNull(token)) {
                try {
                    userService.login(token);
                } catch (RuntimeException e) {
                    Result result = Result.notLogin(e.getMessage());
                    response.getWriter().write(JSON.toJSONString(result));
                    return;
                }
            } else {
                // token 为空 就是未登录
                Result result = Result.notLogin("当前未登录");
                response.getWriter().write(JSON.toJSONString(result));
                return;
            }
        }
        chain.doFilter(request, response);//执行放行
        // 目标资源执行完毕以后再次过滤的地方
        ActiveUserUtil.remove(); // 响应的时候可以移除里面数据
    }
    
    public void init(FilterConfig config) throws ServletException {
        System.out.println("LoginFilter is init ");
        urls = Arrays.asList(config.getInitParameter("urls").split(","));
        System.out.println("LoginFilter:" + urls);
    }
    
}
